const jwt = require("jsonwebtoken");

const {secret} = require("../config/config");

module.exports = (req, res, next) => {
    // 获取请求携带的token
    let token = req.get("token");
    console.log(token)
    if (!token) {
        return res.json({
            code: "2003",
            msg: "token缺失",
            data: {},
        })
    }
    jwt.verify(token, secret, (err, decoded) => {
        if(err) {
            return res.json({
                code: "2004",
                msg: "token无效",
                data: {},
            })
        }
        req.user = decoded;
        next();
    })
}